﻿// ***********************************************************************
// Provided for Informational Purposes Only
//
// Apache 2.0 License
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY 
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR 
// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
// ***********************************************************************
// Assembly  : ContractMeow.Models
// File      : User.cs
//
// ***********************************************************************
using ContractMeow.Api.Dtos.References;
using System;


namespace ContractMeow.Models
{
	/// <summary>
	/// Class User
	/// </summary>
	public class User
	{
		#region Ctors
		/// <summary>
		/// Initializes a new instance of the <see cref="User" /> class.
		/// </summary>
		public User() { }

		/// <summary>
		/// Initializes a new instance of the <see cref="User" /> class.
		/// </summary>
		/// <param name="user">The user.</param>
		public User(ContractMeow.Api.Dtos.User.User user)
		{
			Id = user.Id;
			Name = user.Name;
			FullName = user.FullName;
			JobTitle = user.JobTitle;
			MugshotUri = user.MugshotUrl;
			MugshotUriPattern = user.MugshotUrlTemplate;
			NetworkDomains = user.NetworkDomains;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="User" /> class.
		/// </summary>
		/// <param name="userRef">The user ref.</param>
		public User(UserReference userRef)
		{

			Id = userRef.Id;
			FullName = userRef.FullName;
			JobTitle = userRef.JobTitle;
			Name = userRef.Name;
			MugshotUri = userRef.MugshotUrl;
			MugshotUriPattern = userRef.MugshotUrlTemplate;
		}
		#endregion

		/// <summary>
		/// Gets or sets the id.
		/// </summary>
		/// <value>The id.</value>
		public Int64 Id { get; set; }
		/// <summary>
		/// Gets or sets the name.
		/// </summary>
		/// <value>The name.</value>
		public String Name { get; set; }
		/// <summary>
		/// Gets or sets the full name.
		/// </summary>
		/// <value>The full name.</value>
		public String FullName { get; set; }
		/// <summary>
		/// Gets or sets the job title.
		/// </summary>
		/// <value>The job title.</value>
		public String JobTitle { get; set; }
		/// <summary>
		/// Gets or sets the mugshot URI.
		/// </summary>
		/// <value>The mugshot URI.</value>
		public Uri MugshotUri { get; set; }
		/// <summary>
		/// Gets or sets the mugshot URI pattern.
		/// </summary>
		/// <value>The mugshot URI pattern.</value>
		public String MugshotUriPattern { get; set; }
		/// <summary>
		/// Gets the mugshot uri40x40.
		/// </summary>
		/// <value>The mugshot uri40x40.</value>
		public Uri MugshotUri40x40 { get { return GetMugshotUri(40, 40); } }
		/// <summary>
		/// Gets the mugshot uri45x45.
		/// </summary>
		/// <value>The mugshot uri45x45.</value>
		public Uri MugshotUri45x45 { get { return GetMugshotUri(45, 45); } }
		/// <summary>
		/// Gets the mugshot uri50x50.
		/// </summary>
		/// <value>The mugshot uri50x50.</value>
		public Uri MugshotUri50x50 { get { return GetMugshotUri(50, 50); } }
		/// <summary>
		/// Gets the mugshot uri100x100.
		/// </summary>
		/// <value>The mugshot uri100x100.</value>
		public Uri MugshotUri100x100 { get { return GetMugshotUri(100, 100); } }

		public String[] NetworkDomains { get; set; }

		/// <summary>
		/// Gets the mugshot URI.
		/// </summary>
		/// <param name="width">The width.</param>
		/// <param name="height">The height.</param>
		/// <returns>Uri.</returns>
		public Uri GetMugshotUri(Int32 width, Int32 height)
		{
			if (width <= 0 || height <= 0 || String.IsNullOrWhiteSpace(MugshotUriPattern))
				return MugshotUri;
			return new Uri(MugshotUriPattern
							   .Replace("{width}", width.ToString())
							   .Replace("{height}", height.ToString()));
		}

		/// <summary>
		/// Merges the specified other.
		/// </summary>
		/// <param name="other">The other.</param>
		public void Merge(User other)
		{
			//Leave the Id alone.
			Name = other.Name;
			FullName = other.FullName;
			JobTitle = other.JobTitle;
			MugshotUri = other.MugshotUri;
		}

		public static implicit operator ContractMeow.Api.Dtos.User.User(Models.User user)
		{
			return new ContractMeow.Api.Dtos.User.User()
			{
				Id = user.Id,
				Name = user.Name,
				FullName = user.FullName,
				JobTitle = user.JobTitle,
				MugshotUrl = user.MugshotUri,
				NetworkDomains = user.NetworkDomains
			};
		}

		public static explicit operator Models.User(ContractMeow.Api.Dtos.User.User user)
		{
			return new Models.User(user);
		}
	}
}